package de.sp.games.snake12k;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class TitleMenuState extends BasicGameState
{
	private int stateID = 3;
	private int selected = 0;

	private Image backgroundImage;
	private Image logo;

	private UnicodeFont uFBigger;

	private int time = 0;

	Sound select;

	public TitleMenuState(int stateID)
	{
		this.stateID = stateID;
	}

	public int getID()
	{
		return stateID;
	}

	@SuppressWarnings("unchecked")
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
	{
		backgroundImage = new Image("ressource/box_empty.jpg");
		logo = new Image("ressource/snake12k.png");
		backgroundImage.setFilter(Image.FILTER_NEAREST);
		logo.setFilter(Image.FILTER_NEAREST);

		uFBigger = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 50));
		uFBigger.getEffects().add(new ColorEffect(java.awt.Color.YELLOW));
		uFBigger.addAsciiGlyphs();
		uFBigger.loadGlyphs();

		select = new Sound("ressource/select.wav");
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
	{
		Input input = gc.getInput();
		time++;

		if (input.isKeyPressed(Input.KEY_UP))
		{
			if (selected > 0)
			{
				selected--;
			}
		}
		else if (input.isKeyPressed(Input.KEY_DOWN))
		{
			if (selected < 3)
			{
				selected++;
			}
		}
		else if (input.isKeyPressed(Input.KEY_ENTER))
		{
			switch (selected)
			{
			case 0:
				SUtil.stopPlayedTime();
				SUtil.resetPlayedTime();
				sbg.enterState(Snake12k.GAMEPLAYSTATE);
				select.play();
				input.clearKeyPressedRecord();
				break;
			case 1:
				sbg.enterState(Snake12k.OPTIONSSTATE);
				select.play();
				input.clearKeyPressedRecord();
				break;
			case 2:
				sbg.enterState(Snake12k.HIGHSCORESTATE);
				SUtil.needHighScoreUpdate = true;
				select.play();
				input.clearKeyPressedRecord();
				break;
			case 3:
				select.play();
				System.exit(0);
				input.clearKeyPressedRecord();
				break;
			}
		}
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
	{
		g.scale((1.0f / 16 * gc.getWidth()), (1.0f / 16) * (gc.getHeight() - 30));
		backgroundImage.draw();
		g.resetTransform();

		String msg = "<";

		g.setColor(Color.white);
		switch (selected)
		{
		case 0:
			msg = "Play";
			uFBigger.drawString((gc.getWidth() / 2) + (uFBigger.getWidth(msg) / 2), 200 - (int) Math.abs(Math.sin(time * 0.13) * 10), "<");
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 200 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		case 1:
			msg = "Options";
			uFBigger.drawString((gc.getWidth() / 2) + (uFBigger.getWidth(msg) / 2), 300 - (int) Math.abs(Math.sin(time * 0.13) * 10), "<");
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 300 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		case 2:
			msg = "Highscore";
			uFBigger.drawString((gc.getWidth() / 2) + (uFBigger.getWidth(msg) / 2), 400 - (int) Math.abs(Math.sin(time * 0.13) * 10), "<");
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 400 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		case 3:
			msg = "Exit";
			uFBigger.drawString((gc.getWidth() / 2) + (uFBigger.getWidth(msg) / 2), 500 - (int) Math.abs(Math.sin(time * 0.13) * 10), "<");
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 500 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		}

		/*g.drawString("Play", 10, 10);
		g.drawString("Options", 10, 30);
		g.drawString("Exit", 10, 50);
		*/
		g.setColor(Color.orange);
		g.drawString("Ver: " + Snake12k.version + " by: staxx6 & ThePixel5D", 10, gc.getHeight() - 25);
		logo.draw((gc.getWidth() / 2) - (logo.getWidth() / 2), 50 - (int) Math.abs(Math.sin(time * 0.05) * 5));
		msg = "Play";
		if (selected == 0)
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 200 - (int) Math.abs(Math.sin(time * 0.13) * 10), msg);
		}
		else
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 200, msg);
		}
		msg = "Options";
		if (selected == 1)
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 300 - (int) Math.abs(Math.sin(time * 0.13) * 10), msg);
		}
		else
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 300, msg);
		}
		msg = "Highscore";
		if (selected == 2)
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 400 - (int) Math.abs(Math.sin(time * 0.13) * 10), msg);
		}
		else
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 400, msg);
		}
		msg = "Exit";
		if (selected == 3)
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 500 - (int) Math.abs(Math.sin(time * 0.13) * 10), msg);
		}
		else
		{
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 500, msg);
		}
	}
}
